package com.ulwx.tool;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class RequestUtils {
	private Map<String, Object[]> rParms=new HashMap<String,Object[]>();
	private Map<String, Object[]> objs=new HashMap<String,Object[]>();
	private static Logger log = Logger.getLogger(RequestUtils.class);

	public RequestUtils(Map requestParameters) {
		rParms = requestParameters;
	}

	public RequestUtils() {
		rParms = new HashMap<String, Object[]>();
	}

	public void setString(String name, String value) {
		rParms.put(name, new String[] { value });
	}

	public void setStrings(String name, String[] values) {
		rParms.put(name, values);
	}

	public Map<String, Object[]> getrParms() {
	
		return rParms;
	}

	public void setrParms(Map<String, Object[]> rParms) {
		this.rParms = rParms;
	}

	public void setInt(String name, Integer value) {
		rParms.put(name, new String[] { value + "" });
	}
	public void setObject(String name, Object value) {
		objs.put(name, new Object[] {value });
	}
	
	public void setObjects(String name, Object[] values) {
		objs.put(name, values);
	}
	
	public Object getObject(String name) {
		try {
			Object value = ArrayUtils.getFirst((Object[]) objs.get(name));
			if (value == null)
				return null;
			return value;
		} catch (Exception e) {
			return null;
		}
	}
	public Object[] getObjects(String name) {
		try {
			Object[] value = (Object[]) objs.get(name);
			return value;
		} catch (Exception e) {
			log.error("", e);
			return null;
		}
	}
	public String getString(String name) {
		try {
			String value = ArrayUtils.getFirst((String[]) rParms.get(name));
			if (value == null)
				return "";
			return value;
		} catch (Exception e) {
			return null;
		}
	}

	public File getFile(String name) {
		try {
			File value = ArrayUtils.getFirst((File[]) rParms.get(name));
			return value;
		} catch (Exception e) {
			log.error("", e);
			return null;
		}

	}

	public String getFileContentType(String name) {
		return this.getString(name + "ContentType");
	}

	public String[] getFileContentTypes(String name) {
		return this.getStrings(name + "ContentType");
	}

	public String getFileName(String name) {
		return this.getString(name + "FileName");
	}

	public String[] getFileNames(String name) {
		return this.getStrings(name + "FileName");
	}

	public File[] getFiles(String name) {
		try {
			return (File[]) rParms.get(name);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}

	}

	public Integer getInt(String name) {
		String value = ArrayUtils.getFirst((String[]) rParms.get(name));
		if (value == null)
			return null;
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}
	}

	public Byte getByte(String name) {
		String value = ArrayUtils.getFirst((String[]) rParms.get(name));
		if (value == null)
			return null;
		try {
			return Byte.parseByte(value);
		} catch (Exception e) {
			log.error("", e);
			// TODO Auto-generated catch block
			return null;
		}
	}

	public Boolean getBoolean(String name) {
		String value = ArrayUtils.getFirst((String[]) rParms.get(name));
		if (value == null)
			return null;
		try {
			return Boolean.valueOf(value);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}

	}

	public String[] getStrings(String name) {
		try {
			String[] value = (String[]) rParms.get(name);
			return value;
		} catch (Exception e) {
			log.error("", e);
			return null;
		}
	}

	public void setInts(String name, Integer[] values) {
		rParms.put(name, ArrayUtils.toStringArray(values));
	}

	public void setBoolean(String name, Boolean value) {
		rParms.put(name, new String[] { value + "" });
	}

	public void setBooleans(String name, Boolean[] values) {
		rParms.put(name, ArrayUtils.toStringArray(values));
	}

	public void setBytes(String name, Byte[] values) {
		rParms.put(name, ArrayUtils.toStringArray(values));
	}

	public void setByte(String name, Byte value) {
		rParms.put(name, new String[] { value + "" });
	}

	public Date getDayDate(String name) {
		String value = ArrayUtils.getFirst((String[]) rParms.get(name));
		if (value == null)
			return null;
		return CTime.parseDayDate(value);
	}

	public Date[] getDayDates(String name) {
		String[] value = (String[]) rParms.get(name);
		if (value == null)
			return null;
		Date[] res = new Date[value.length];
		for (int i = 0; i < value.length; i++) {
			res[i] = CTime.parseDayDate(value[i]);
		}

		return res;
	}

	public void setWholeDate(String name, Date value) {
		String str = CTime.formatWholeDate(value);

		rParms.put(name, new String[] { str });
	}

	public void setWholeDates(String name, Date[] values) {
		String[] strs = new String[values.length];
		for (int i = 0; i < strs.length; i++) {
			strs[i] = CTime.formatWholeDate(values[i]);
		}
		rParms.put(name, strs);
	}

	public Date getWholeDate(String name) {
		String value = ArrayUtils.getFirst((String[]) rParms.get(name));
		if (value == null)
			return null;
		return CTime.parseWholeDate(value);
	}

	public Date[] getWholeDates(String name) {
		String[] value = (String[]) rParms.get(name);
		if (value == null)
			return null;
		Date[] res = new Date[value.length];
		for (int i = 0; i < value.length; i++) {
			res[i] = CTime.parseWholeDate(value[i]);
		}

		return res;
	}

	public Integer[] getInts(String name) {
		String[] value = (String[]) rParms.get(name);
		if (value == null)
			return null;
		Integer[] res = new Integer[value.length];
		for (int i = 0; i < value.length; i++) {
			try {
				res[i] = Integer.parseInt(value[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("", e);
				res[i] = null;
			}
		}

		return res;
	}

	public Boolean[] getBooleans(String name) {
		String[] value = (String[]) rParms.get(name);
		if (value == null)
			return null;
		Boolean[] res = new Boolean[value.length];
		for (int i = 0; i < value.length; i++) {
			try {
				res[i] = Boolean.valueOf(value[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				res[i] = null;
			}
		}

		return res;
	}

	public Byte[] getBytes(String name) {
		String[] value = (String[]) rParms.get(name);
		if (value == null)
			return null;
		Byte[] res = new Byte[value.length];
		for (int i = 0; i < value.length; i++) {
			try {
				res[i] = Byte.parseByte(value[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				res[i] = null;
			}
		}

		return res;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		RequestUtils ru = new RequestUtils();
		ru.setBooleans("sss", new Boolean[] { true, false });
		System.out.println(ArrayUtils.toJsonString(ru.getBooleans("sss")));

	}

}
